﻿
using System;
using System.Linq;
namespace sharpxbot.Core.Security.Traffic.Counters
{
    /// <summary>
    /// Represents hour counters. Each hour contains 60 minute counters
    /// </summary>
    public class HourCounters : BaseCounters
    {
        /// <summary>
        /// Gets hour value
        /// </summary>
        public int Hour { get; private set; }

        /// <summary>
        /// Gets counters for each minute
        /// </summary>
        public long[] Minutes { get; private set; }

        /// <summary>
        /// Creates new hour counters using given hour value and array of minutes
        /// </summary>
        /// <param name="hour">Must be between 0 and 23 (all bounds inclusive)</param>
        /// <param name="minutes">Must be valid 60-items length array</param>
        public HourCounters(int hour, long[] minutes)
            :base()
        {
            Validator.ValidateHour(hour);

            if (minutes == null)
                throw new NullReferenceException("Illegal minutes array. Null-reference");

            if (minutes.Length < Constants.MinutesCount)
                throw new ArgumentException(String.Format("Illegal minutes array. Must be {0} length", Constants.MinutesCount));

            this.Hour = hour;
            this.Minutes = minutes;
        }

        /// <summary>
        /// Creates new hour counters using given hour
        /// </summary>
        /// <param name="hour">Must be between 0 and 23 (all bounds inclusive)</param>
        public HourCounters(int hour)
            : this(hour, new long[Constants.MinutesCount]) { }

        /// <summary>
        /// Creates new hour counters using current system time
        /// </summary>
        public HourCounters()
            : this(DateTime.Now.Hour, new long[Constants.MinutesCount]) { }

        /// <summary>
        /// Calculates total count of bytes in counter
        /// </summary>
        /// <returns></returns>
        public long CalculateTotal()
        {
            return Minutes.Sum();
        }
    }
}
